package patrol.mobile.com.mobilepatrol.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.bridge.BridgeListActivity;
import patrol.mobile.com.mobilepatrol.activity.culvert.CulvertListActivity;
import patrol.mobile.com.mobilepatrol.activity.office.NewsListActivity;
import patrol.mobile.com.mobilepatrol.activity.office.OtherBusinessActivity;
import patrol.mobile.com.mobilepatrol.activity.office.OtherBusinessSearchActivity;
import patrol.mobile.com.mobilepatrol.activity.road_block.RoadBlockActivity;
import patrol.mobile.com.mobilepatrol.activity.road_survey.TrafficInvestActivity;
import patrol.mobile.com.mobilepatrol.activity.route_query.RouteActivity;
import patrol.mobile.com.mobilepatrol.activity.route_query.TotalRouteActivity;
import patrol.mobile.com.mobilepatrol.activity.track.TrackListActivity;
import patrol.mobile.com.mobilepatrol.activity.yanghuke.lukuangxinxi.LuKuangXinXiActivity;
import patrol.mobile.com.mobilepatrol.activity.yanghuke.niandufeiyong.NianDuFeiyongActivity;
import patrol.mobile.com.mobilepatrol.activity.yanghuke.zuoyefenbu.ZuoyeFenBuActivity;
import patrol.mobile.com.mobilepatrol.activity.yanghuke.zuoyeshangbao.ZuoyeListActivity;
import patrol.mobile.com.mobilepatrol.bean.CygnBean;
import patrol.mobile.com.mobilepatrol.bean.MenuConstants;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;

/**
 * Created by z on 2018/10/6.
 * Des: 编辑常用功能
 */

public class CygnActivity extends BaseActivity {
    @BindView(R.id.cygnRecyclerView)
    RecyclerView cygnRecyclerView;
    @BindView(R.id.allRecyclerView)
    RecyclerView allRecyclerView;
    private List<CygnBean> allcygnBeans;
    private List<CygnBean> selectBeans;
    private ContentAdapter allContentAdapter;
    private SelectContentAdapter cygnContentAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_cygn_layout;
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("功能筛选");
        getAllData();
        getCyxxData();
    }

    //全部功能  的数据存储
    private void getAllData() {
        String ALLDATA = (String) SPUtils.get(UserManager.getUserId() + SPUtils.ALLDATA, "");
        if (TextUtils.isEmpty(ALLDATA)) {


            List allcygnBeans = new ArrayList<>();
            String[] names = {
                    /*7*/"单位概况", "党建信息", "公路养护", "收费服务", "安全生产", "访聚会", "考核汇总",
                    /*7*/"路线查询", "桥梁信息", "涵洞信息", "作业分布", "通阻信息", "路况信息", "年度计划费用",
                    "机电设备台账", "绩效考核汇总",
//                    "备品备件",
                    "机电故障汇总", "机电巡检汇总", "岗位考核", "我的考核", "机电巡检", "故障报修", "机电修复",
                    "应急保障基地", "应急机制", "应急预案体系", "应急演练",
                    "应急检查",
                    "应急事件统计", "安全管理考核汇总",
                    "安全生产组织机构", "安全教育",
//                    "安全事故处理",
                    "安全检查", "作业区专项检查",
                    "定期检查",
                    "外包工程安全管理",
                    "护队花名册", "维稳器材汇总", "节日值班表", "值带班检查",
                    "综合维稳定期检查",
//                    "保联保户",
//                    "防爆汇总1",
//                    "防爆汇总2",
                    "包联领导",
                    "包联编辑"};
            String[] menusArray = concatAll(MenuConstants.YHGL_CHILD_TITLES_1,
                    MenuConstants.YHGL_CHILD_TITLES_2,
                    MenuConstants.JKGL_CHILD_TITLES_1,
                    MenuConstants.JKGL_CHILD_TITLES_2,
                    MenuConstants.ABGL_TAB1_CHILD_TITLES_1,
                    MenuConstants.ABGL_TAB1_CHILD_TITLES_2,
                    MenuConstants.ABGL_TAB1_CHILD_TITLES_3,
                    MenuConstants.ABGL_TAB1_CHILD_TITLES_4,
                    MenuConstants.ABGL_TAB2_CHILD_TITLES_1,
                    MenuConstants.ABGL_TAB2_CHILD_TITLES_2,
                    MenuConstants.ABGL_TAB2_CHILD_TITLES_3,
                    MenuConstants.ABGL_TAB2_CHILD_TITLES_4,
                    MenuConstants.FEEGL_CHILD_TITLES_1,
                    MenuConstants.FEEGL_CHILD_TITLES_2);
            int[] resArray = concatAll2(MenuConstants.YHGL_CHILD_IMGS_1,
                    MenuConstants.YHGL_CHILD_IMGS_2,
                    MenuConstants.JKGL_CHILD_IMGS_1,
                    MenuConstants.JKGL_CHILD_IMGS_2,
                    MenuConstants.ABGL_TAB1_CHILD_IMG_1,
                    MenuConstants.ABGL_TAB1_CHILD_IMG_2,
                    MenuConstants.ABGL_TAB1_CHILD_IMG_3,
                    MenuConstants.ABGL_TAB1_CHILD_IMG_4,
                    MenuConstants.ABGL_TAB2_CHILD_IMG_1,
                    MenuConstants.ABGL_TAB2_CHILD_IMG_2,
                    MenuConstants.ABGL_TAB2_CHILD_IMG_3,
                    MenuConstants.ABGL_TAB2_CHILD_IMG_4,
                    MenuConstants.FEEGL_CHILD_IMGS_IMG_1,
                    MenuConstants.FEEGL_CHILD_IMGS_IMG_2);
//            Map<String, Map<String,Integer>> menus=new HashMap<>();
//            LinkedHashMap<String, Integer> menus = new LinkedHashMap<>();
////            menus.putAll(manageMenuData(MenuConstants.XZGL_CHILD_TITLES_1,MenuConstants.XZGL_CHILD_IMGS_1));
//            menus.putAll(manageMenuData(MenuConstants.YHGL_CHILD_TITLES_1, MenuConstants.YHGL_CHILD_IMGS_1));
//            menus.putAll(manageMenuData(MenuConstants.YHGL_CHILD_TITLES_2, MenuConstants.YHGL_CHILD_IMGS_2));
//            menus.putAll(manageMenuData(MenuConstants.JKGL_CHILD_TITLES_1, MenuConstants.JKGL_CHILD_IMGS_1));
//            menus.putAll(manageMenuData(MenuConstants.JKGL_CHILD_TITLES_2, MenuConstants.JKGL_CHILD_IMGS_2));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB1_CHILD_TITLES_1, MenuConstants.ABGL_TAB1_CHILD_IMG_1));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB1_CHILD_TITLES_2, MenuConstants.ABGL_TAB1_CHILD_IMG_2));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB1_CHILD_TITLES_3, MenuConstants.ABGL_TAB1_CHILD_IMG_3));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB1_CHILD_TITLES_4, MenuConstants.ABGL_TAB1_CHILD_IMG_4));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB2_CHILD_TITLES_1, MenuConstants.ABGL_TAB2_CHILD_IMG_1));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB2_CHILD_TITLES_2, MenuConstants.ABGL_TAB2_CHILD_IMG_2));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB2_CHILD_TITLES_3, MenuConstants.ABGL_TAB2_CHILD_IMG_3));
//            menus.putAll(manageMenuData(MenuConstants.ABGL_TAB2_CHILD_TITLES_4, MenuConstants.ABGL_TAB2_CHILD_IMG_4));
//            menus.putAll(manageMenuData(MenuConstants.FEEGL_CHILD_TITLES_1, MenuConstants.FEEGL_CHILD_IMGS_IMG_1));
//            menus.putAll(manageMenuData(MenuConstants.FEEGL_CHILD_TITLES_2, MenuConstants.FEEGL_CHILD_IMGS_IMG_2));
//            int[] isons = {R.mipmap.unit_profile, R.mipmap.party_information, R.mipmap.highway_maintenance, R.mipmap.charge_service, R.mipmap.safety_production, R.mipmap.visit_gather, R.mipmap.assess_pool,
//                    R.mipmap.l6, R.mipmap.l4, R.mipmap.l1, R.mipmap.l3, R.mipmap.l5, R.mipmap.l6, R.mipmap.l4,
//                    R.mipmap.l1,
//                    R.mipmap.equipment_file, R.mipmap.assess_pool, R.mipmap.preparation, R.mipmap.fault_pool, R.mipmap.mechatronics_ins_sum, R.mipmap.other_queries, R.mipmap.post_assessment, R.mipmap.my_assessment, R.mipmap.mechatronic_inspection, R.mipmap.failure_reporting, R.mipmap.mechanical_electrical_repair,
//                    R.mipmap.emergency_base, R.mipmap.emergency_mechanism, R.mipmap.ntingency_plan, R.mipmap.emergency_rehearsal, R.mipmap.security_check, R.mipmap.emergency_statistics, R.mipmap.assess_pool, R.mipmap.organization, R.mipmap.safety_education, R.mipmap.accident_treatment, R.mipmap.accident_treatment, R.mipmap.chance_inspect, R.mipmap.periodic_inspection, R.mipmap.ectocy_administration, R.mipmap.roster, R.mipmap.equipment_summary, R.mipmap.holiday_rota, R.mipmap.check_duty, R.mipmap.comprehensive_examination, R.mipmap.packs, R.mipmap.comprehensive_examination, R.mipmap.ntingency_plan, R.mipmap.emergency_statistics, R.mipmap.chance_inspect};
            String xingzhengguanli = NewsListActivity.class.getName();
            String[] target = {
                    /**7**/xingzhengguanli, xingzhengguanli, xingzhengguanli, xingzhengguanli, xingzhengguanli, xingzhengguanli, xingzhengguanli,
                    /*****/TotalRouteActivity.class.getName() + "?" + RouteActivity.class.getName(), BridgeListActivity.class.getName(),
                    /**7**/CulvertListActivity.class.getName(), ZuoyeFenBuActivity.class.getName(), RoadBlockActivity.class.getName(),
                    /*****/LuKuangXinXiActivity.class.getName(),/*OtherBusinessSearchActivity.class.getName(),*/NianDuFeiyongActivity.class.getName(),

//                    TrafficInvestActivity.class.getName(),
//                    TrackListActivity.class.getName(),
//                    BridgeListActivity.class.getName(),
//                    CulvertListActivity.class.getName(),
//                    RoadBlockActivity.class.getName(),
//                    ZuoyeListActivity.class.getName(),
//                    OtherBusinessActivity.class.getName(),
            };
//            String[] clazz = {
//                    target,
//
//            };
//            Set<String> stringSet = menus.keySet();
//            int index = 0;
//            for (String s : stringSet) {
//                CygnBean cygnBean = new CygnBean();
//                cygnBean.setIcon(menus.get(s));
//                cygnBean.setTitle(s);
//                cygnBean.setIntdex(index);
//                cygnBean.setTarget(target[0]);
//                allcygnBeans.add(cygnBean);
//                index++;
//            }
            for (int i = 0; i < menusArray.length; i++) {
                CygnBean cygnBean = new CygnBean();
                cygnBean.setIcon(resArray[i]);
                cygnBean.setTitle(menusArray[i]);
                cygnBean.setIntdex(i);
                cygnBean.setTarget(target[0]);
                allcygnBeans.add(cygnBean);
            }
            SPUtils.put(this, UserManager.getUserId() + SPUtils.ALLDATA, new Gson().toJson(allcygnBeans));
        }
        allcygnBeans = GsonUtil.jsonToList((String) SPUtils.get(UserManager.getUserId() + SPUtils.ALLDATA, ""), CygnBean.class);
        allRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        allRecyclerView.setAdapter(allContentAdapter = new ContentAdapter());
    }

    private Map<String, Integer> manageMenuData(String[] strings, int[] resId) {
        Map<String, Integer> menus = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            menus.put(strings[i], resId[i]);
        }
        return menus;
    }

    public static <T> T[] concatAll(T[] first, T[]... rest) {

        int totalLength = first.length;

        for (T[] array : rest) {

            totalLength += array.length;

        }

        T[] result = Arrays.copyOf(first, totalLength);

        int offset = first.length;

        for (T[] array : rest) {

            System.arraycopy(array, 0, result, offset, array.length);

            offset += array.length;

        }

        return result;

    }

    public static int[] concatAll2(int[] first, int[]... rest) {

        int totalLength = first.length;

        for (int[] array : rest) {

            totalLength += array.length;

        }

        int[] result = Arrays.copyOf(first, totalLength);

        int offset = first.length;

        for (int[] array : rest) {

            System.arraycopy(array, 0, result, offset, array.length);

            offset += array.length;

        }

        return result;

    }
    //获取常用信息
    private void getCyxxData() {
        String CYXX = (String) SPUtils.get(UserManager.getUserId() + SPUtils.CYXXDATA, "");
        if (TextUtils.isEmpty(CYXX)) {
            selectBeans = new ArrayList<>();
        } else {
            selectBeans = GsonUtil.jsonToList(CYXX, CygnBean.class);
        }
        cygnRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        cygnRecyclerView.setAdapter(cygnContentAdapter = new SelectContentAdapter());

    }


    public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {

        @Override
        public ContentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_content_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ContentAdapter.ViewHolder holder, final int position) {
            holder.itemTet.setText(allcygnBeans.get(position).getTitle());
            holder.itemImage.setImageResource(allcygnBeans.get(position).getIcon());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectBeans.add(allcygnBeans.get(position));
                    allcygnBeans.remove(position);
                    SPUtils.put(v.getContext(), UserManager.getUserId() + SPUtils.ALLDATA, new Gson().toJson(allcygnBeans));
                    SPUtils.put(v.getContext(), UserManager.getUserId() + SPUtils.CYXXDATA, new Gson().toJson(selectBeans));
                    cygnContentAdapter.notifyDataSetChanged();
                    allContentAdapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return allcygnBeans == null ? 0 : allcygnBeans.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.itemImage)
            ImageView itemImage;
            @BindView(R.id.itemTet)
            TextView itemTet;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }

    public class SelectContentAdapter extends RecyclerView.Adapter<SelectContentAdapter.ViewHolder> {

        @Override
        public SelectContentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_content_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(SelectContentAdapter.ViewHolder holder, final int position) {
            holder.itemTet.setText(selectBeans.get(position).getTitle());
            holder.itemImage.setImageResource(selectBeans.get(position).getIcon());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    allcygnBeans.add(0, selectBeans.get(position));
                    selectBeans.remove(position);
                    SPUtils.put(v.getContext(), UserManager.getUserId() + SPUtils.ALLDATA, new Gson().toJson(allcygnBeans));
                    SPUtils.put(v.getContext(), UserManager.getUserId() + SPUtils.CYXXDATA, new Gson().toJson(selectBeans));
                    cygnContentAdapter.notifyDataSetChanged();
                    allContentAdapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return selectBeans == null ? 0 : selectBeans.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.itemImage)
            ImageView itemImage;
            @BindView(R.id.itemTet)
            TextView itemTet;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
